package rsj.service.export.product;

import com.alibaba.excel.EasyExcelFactory;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.fastjson.JSON;
import rsj.api.product.entity.ProductUsageDownloadExportEntity;
import rsj.service.export.IExportTemplateMethod;

import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
import java.util.List;

/**
 * 产品管理-导出详情、下载详情相关
 *
 * @author yuanqinglong
 * @since 2021/3/5 15:34
 */
public interface IProductDownloadDetail extends IExportTemplateMethod {


    /**
     * 将生成的文件写入本地
     *
     * @param jsonString 查询的数据
     * @param sheetName  导出的工作表名称
     * @return 写入文件的路径
     */
    @Override
    default OutputStream writeDataToTheOutputStream(String jsonString, String sheetName) {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        List<ProductUsageDownloadExportEntity> downloadExportEntities = JSON.parseArray(jsonString, ProductUsageDownloadExportEntity.class);
        EasyExcelFactory.write(outputStream, ProductUsageDownloadExportEntity.class).sheet().doWrite(downloadExportEntities);
        return outputStream;
    }


    /**
     * 写数据到excel
     *
     * @param jsonData    json数据
     * @param excelWriter 优秀的作家
     * @param sheetNum    表num
     */
    @Override
    default void writeDataToTheExcelWriter(String jsonData, ExcelWriter excelWriter, int sheetNum) {
        WriteSheet writeSheet = EasyExcelFactory.writerSheet(sheetNum).head(ProductUsageDownloadExportEntity.class).build();
        excelWriter.write(JSON.parseArray(jsonData, ProductUsageDownloadExportEntity.class), writeSheet);
    }
}
